from rest_framework import serializers
from tensquare.apps.user.models import User

class LabelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ["id", "label_name"]

class QuestionSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField(read_only=True)
    labels = serializers.StringRelatedField(read_only=True, many=True)

    class Meta:
        model = Question
        fields = ["id", "createtime", "labels", "reply", "replyname", "replytime", "title", "unuseful_count",
                  "useful_count", "user", "visits"]


class UserSerializerBase(serializers.ModelSerializer):

    class Meta:
        model = User
        fields = ('id', 'username', 'avatar')

class ReplySerializerSubAndParent(serializers.ModelSerializer):

    user = UserSerializerBase(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count","unuseful_count","user"]


class ReplySerializer(serializers.ModelSerializer):

    user = UserSerializerBase(read_only=True)
    subs = ReplySerializerSubAndParent(read_only=True, many=True)
    parent = ReplySerializerSubAndParent(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content","createtime","useful_count",'problem',"unuseful_count","subs","user","parent"]


class ArticleSerializer(serializers.ModelSerializer):
    user = UserSerializerBase(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ("id", "title","content","createtime","user","collected_users","collected","image","visits")



class EnterpriseSerializer(serializers.ModelSerializer):

    class Meta:
        model = Enterprise
        fields = ('id', 'name','labels','logo','recruits','summary')





class UserInfoSerializer(serializers.ModelSerializer):
    """
    用户详细信息序列化器
    """
    labels = LabelSerializer(required=False, many=True)
    username = serializers.CharField(read_only=True)
    questions = QuestionSerializer(read_only=True, many=True)
    answer_question = ReplySerializer(read_only=True, many=True)
    collected_articles = ArticleSerializer(read_only=True, many=True)
    enterpises = EnterpriseSerializer(read_only=True, many=True)

    class Meta:
        model = User
        fields = ('id', 'username', 'mobile','realname','birthday','sex','avatar','website','email','city', 'address','labels','questions','answer_question','collected_articles','enterpises')




class UserPasswordSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('password',)
        extra_kwargs = {
            'password': {'write_only': True}
        }

    def update(self, instance, validated_data):
        user = super().update(instance, validated_data)
        user.set_password(validated_data['password'])
        user.save()
        return user

class UserLabelSerializer(serializers.ModelSerializer):

    labels = serializers.PrimaryKeyRelatedField(required=True, many=True, queryset=Label.objects.all())

    class Meta:
        model = User
        fields = ('id', 'labels')